package com.jingtai.platform.service.supply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.supply.SupplyGoodsRule;
import com.jingtai.core.mapper.supply.SupplyGoodsRuleMapper;
import com.jingtai.platform.service.supply.SupplyGoodsRuleService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 供应商售卖规则表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:32
 */
@Service
public class SupplyGoodsRuleServiceImpl extends ServiceImpl<SupplyGoodsRuleMapper, SupplyGoodsRule> implements SupplyGoodsRuleService {

    @Autowired
    private SupplyGoodsRuleService supplyGoodsRuleService;

    @Override
    public ResultUtil SupplyGoodsRulelist(Integer page, Integer limit, String conditions){
        QueryWrapper<SupplyGoodsRule> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsRuleService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addSupplyGoodsRule(SupplyGoodsRule supplyGoodsRule) {
        boolean save = supplyGoodsRuleService.save(supplyGoodsRule);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getSupplyGoodsRuleById(Long id) {
        SupplyGoodsRule supplyGoodsRule = supplyGoodsRuleService.getById(id);
        return supplyGoodsRule == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsRule);
    }

    @Override
    public ResultUtil delSupplyGoodsRuleById(Long id) {
        SupplyGoodsRule supplyGoodsRule = new SupplyGoodsRule();
        supplyGoodsRule.setId(id);
        supplyGoodsRule.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = supplyGoodsRuleService.updateById(supplyGoodsRule);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil editSupplyGoodsRule(SupplyGoodsRule supplyGoodsRule) {
        boolean editState = supplyGoodsRuleService.updateById(supplyGoodsRule);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
